package com.rstrulak.managedBeans.controllers;

/**
 * 
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.rstrulak.model.NoteAttachments;
import com.rstrulak.model.ProjectAttachments;
import com.rstrulak.model.TaskAttachments;
import com.rstrulak.utils.JSFUtils;

@Component("fileDownloadBean")
@Scope("request")
public class FileDownloadBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4182783587329634387L;

	@PostConstruct
	public void init(){
		JSFUtils.setSessionParams();
	}

	public void saveNoteFile(NoteAttachments noteAttachments) throws IOException {
		String filename = noteAttachments.getFileName();

		byte[] bytesArray = readFile(noteAttachments.getFilePath());

		FacesContext faces = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
		response.setContentLength(bytesArray.length);
		response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
		try {
			ServletOutputStream out;
			out = response.getOutputStream();
			out.write(bytesArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSFUtils.reloadFlash();
		faces.responseComplete();
		JSFUtils.reloadFlash();
	}
	

	public void saveTaskFile(TaskAttachments taskAttachments) throws IOException {
		String filename = taskAttachments.getFileName();

		byte[] bytesArray = readFile(taskAttachments.getFilePath());

		FacesContext faces = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
		response.setContentLength(bytesArray.length);
		response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
		try {
			ServletOutputStream out;
			out = response.getOutputStream();
			out.write(bytesArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSFUtils.reloadFlash();
		faces.responseComplete();
		JSFUtils.reloadFlash();
	}
	

	public void saveProjectFile(ProjectAttachments projectAttachments) throws IOException {
		String filename = projectAttachments.getFileName();

		byte[] bytesArray = readFile(projectAttachments.getFilePath());

		FacesContext faces = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
		response.setContentLength(bytesArray.length);
		response.setHeader("Content-disposition", "attachment; filename=\"" + filename + "\"");
		try {
			ServletOutputStream out;
			out = response.getOutputStream();
			out.write(bytesArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSFUtils.reloadFlash();
		faces.responseComplete();
		JSFUtils.reloadFlash();
	}



	public void viewFile(NoteAttachments noteAttachments) throws IOException {
		String filename = noteAttachments.getFileName();

		byte[] bytesArray = readFile(noteAttachments.getFilePath());

		FacesContext faces = FacesContext.getCurrentInstance();
		HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
		response.setContentLength(bytesArray.length);
		response.setHeader("Content-disposition", "inline; filename=\"" + filename + "\"");
		try {
			ServletOutputStream out;
			out = response.getOutputStream();
			out.write(bytesArray);
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSFUtils.reloadFlash();
		faces.responseComplete();
		JSFUtils.reloadFlash();
	}

	public byte[] readFile(String filePath) throws IOException {
		File file = new File(filePath);

		FileInputStream fin = new FileInputStream(file);

		byte fileContent[] = new byte[(int) file.length()];

		fin.read(fileContent);

		return fileContent;

	}

}
